package com.server.api.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.server.api.common.ReturnMsg;
import com.server.api.common.constants.BaseConstant;
import com.server.api.dto.${className}DTO;
import com.server.api.service.${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 后台登录
 * @author: wuming
 **/
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ${className}Service ${unClassName}Service;

    @PostMapping()
    public ReturnMsg login(@RequestBody ${className}DTO ${unClassName}DTO) {
        Assert.isTrue(StrUtil.isNotEmpty(${unClassName}DTO.get${userNameCol}())
                && StrUtil.isNotEmpty(${unClassName}DTO.get${passWordCol}()), "账号或密码不能为空!");
        LambdaQueryWrapper<${className}DTO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(${className}DTO::get${userNameCol},${unClassName}DTO.get${userNameCol}()).
                eq(${className}DTO::get${passWordCol},${unClassName}DTO.get${passWordCol}()).last(BaseConstant.LIMIT_ONE);
        ${className}DTO loginUser = ${unClassName}Service.getOne(queryWrapper);
        Assert.notNull(loginUser,"账号或密码错误!");
        return ReturnMsg.ok(loginUser);
    }

    @GetMapping("/{id}")
    public ReturnMsg getUserInfo(@PathVariable("id") Long id) {
    List<String> roles = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        roles.add("admin");
        map.put("roles", roles);
        map.put("introduction", "打工人...");
        <#if !hasUserInfo>
        map.put("name", "admin");
        map.put("avatar", "http://www.elitetest.cn/img/logo.png");
            <#else>
        ${className}DTO userInfo = ${unClassName}Service.getById(id);
        map.put("name", userInfo.get${nickNameCol}());
        map.put("avatar", StrUtil.isNotEmpty(userInfo.get${headImgCol}()) ? userInfo.get${headImgCol}() : "http://www.elitetest.cn/img/logo.png");
        </#if>
        return ReturnMsg.ok(map);
    }

    @DeleteMapping()
    public ReturnMsg logOut() {
        return ReturnMsg.ok();
    }

}
